import {
  GetOrderList,
  DeliveryBatch,
  GetPrintItemConfig,
  SaveLPrintConfig,
  GetDeliveryManList,
  GetDeliveryManPageList,
  DeleteMan,
  AddMan
} from '@/services/order/localdelivery';
import {catchException} from '@/utils/utils';
export default {
  namespace: 'localdelivery',

  state: {
    orderList:[],
    deliveryManPageList:[]
  },

  effects: {

      // 获取同城配送待配送订单列表
      *GetOrderList({payload, callBack}, {call, put}) {
          const data = yield call(GetOrderList, payload);
          yield catchException(function* (){
            if(data&&data.Code === 0) {
                yield put({
                    type: 'saveGetOrderList',
                    payload: {
                        dataList: data.Data,
                    },
                });
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data);
            }
          })           
      },

      //订单批量发货
      *DeliveryBatch({payload, callBack}, {call, put}) {
            const data = yield call(DeliveryBatch, payload);
            yield catchException(function* (){
                if(data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data)
                }
            })        
      },
      // 获取打印小票配置
      *GetPrintItemConfig({payload, callBack}, {call, put}) {
            const data = yield call(GetPrintItemConfig, payload);
            yield catchException(function* (){
                if(data&&data.Code === 0) {
                    callBack && callBack.success(data);
                } else {
                    callBack && callBack.error(data);
                }
            })        
    },
    //保存打印配置
    *SaveLPrintConfig({payload, callBack}, {call, put}) {
        const data = yield call(SaveLPrintConfig, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data)
            }
        })      
    },
    // 获取配送员列表
    *GetDeliveryManList({payload, callBack}, {call, put}) {
        const data = yield call(GetDeliveryManList, payload);
        yield catchException(function* (){
            if(data&&data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data);
            }
        })      
    },
    // 获取配送员分页列表
    *GetDeliveryManPageList({payload, callBack}, {call, put}) {
        const data = yield call(GetDeliveryManPageList, payload);
        yield catchException(function* (){
            if(data&&data.Code === 0) {
                yield put({
                  type: 'saveDeliveryManPageList',
                  payload: {
                      dataList: data.Data,
                  },
                });
                callBack && callBack.success(data);
              } else {
                callBack && callBack.error(data);
              }
        })      
    },
    // 删除配送员（可批量）
    *DeleteMan({payload, callBack}, {call, put}) {
        const data = yield call(DeleteMan, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data);
            }
        })      
    },
    // 新增配送员
    *AddMan({payload, callBack}, {call, put}) {
        const data = yield call(AddMan, payload);
        yield catchException(function* (){
            if(data.Code === 0) {
                callBack && callBack.success(data);
            } else {
                callBack && callBack.error(data);
            }
        })      
    },

  },

  reducers: {
    saveGetOrderList(state, {payload}) {
        return {
            ...state,
            orderList: payload.dataList,
        };
    },
    saveDeliveryManPageList(state, {payload}) {
      return {
          ...state,
          deliveryManPageList: payload.dataList,
      };
    },
  }
}
